import type { App } from 'vue'
import request from './axios.config'
import { filterData } from '@/utils'
export interface HttpOption {
  url: string
  data?: any
  method?: string
  headers?: any
  beforeRequest?: () => void
  afterRequest?: () => void
}

export interface Response<T = any> {
  totalSize: number | 0
  code: number
  msg: string
  data: T
}

export function get(url: string, data?: any, config: any = {}): Promise<Response> {
  // 过滤传参数据
  const _data = filterData(data, config.isFilterData, true)
  return request.get(url, { params: _data, ...config })
}

export function post(url: string, data?: any, config: any = {}): Promise<Response> {
  // 过滤传参数据
  const isFilterData = config.isFilterData === undefined || config.isFilterData === null ? false : config.isFilterData
  const _data = filterData(data, isFilterData, false)
  return request.post(url, _data, { ...config })
}
export function put(url: string, data?: any, config: any = {}): Promise<Response> {
  return request.put(url, data, { ...config })
}

function install(app: App): void {
  app.config.globalProperties.$get = get
  app.config.globalProperties.$post = post
}

export default {
  install,
  get,
  post,
}
